<template>
  <transition name="slide">
    <music-list :title="title" :bg-image="bgImage" :songs="songs"></music-list>
  </transition>
</template>

<script>
import MusicList from '@/components/music-list/music-list'
import { mapGetters } from 'vuex'
import { getRecommendListDetail } from '@/api/recommend'
import { ERR_OK } from '@/common/js/common'
import { createSong } from '@/common/js/song'

export default {
  data () {
    return {
      songs: []
    }
  },
  components: {
    MusicList
  },
  computed: {
    title () {
      return this.disc.name
    },
    bgImage () {
      return this.disc.picUrl
    },
    ...mapGetters([
      'disc'
    ])
  },
  methods: {
    _getRecommendListDetail () {
      if (!this.disc.id) {
        this.$router.push('/lunbo')
        this.$router.go(0)
      }
      getRecommendListDetail(this.disc.id).then((res) => {
        if (res.status === ERR_OK) {
          // console.log(res.data.playlist)
          // console.log(res.data.playlist.tracks)
          this.songs = this._normalizeSongs(res.data.playlist.tracks)
        }
      })
    },
    _normalizeSongs (list) {
      var ret = []
      list.forEach((musicData) => {
        if (musicData.id) {
          ret.push(createSong(musicData))
        }
      })
      return ret
    }
  },
  created () {
    this._getRecommendListDetail()
    // console.log(this.songs)
  },
  watch: {
    $route () {
      this._getRecommendListDetail()
    }
  }
}
</script>

<style lang="less" scoped>
.slide-enter-active, .slide-leave-active {
  transition: all 0.3s;
}
.slide-enter, .slide-leave-to {
  transform: translate3d(100%, 0, 0);
}
</style>
